import * as cc from "cc";
import ModelGuide from "../../game/mvc/guide/model/ModelGuide";
import { GameEventDefine } from "../event/GameEventDefine";
import { Loger } from "../loger/Loger";
import ViewMgr from "../view/ViewMgr";

const { ccclass, menu, property } = cc._decorator;
@ccclass
    @menu("扩展组件/GameManager")
export default class GameManager extends cc.Component {

    onLoad() {
        cc.director.addPersistRootNode(this.node);
        // // cc.PhysicsSystem2D.instance.debugDrawFlags = cc.EPhysics2DDrawFlags.Aabb |
        // //     cc.EPhysics2DDrawFlags.Pair |
        // //     cc.EPhysics2DDrawFlags.CenterOfMass |
        // //     cc.EPhysics2DDrawFlags.Joint |
        // //     cc.EPhysics2DDrawFlags.Shape;
        // cc.PhysicsSystem2D.instance.gravity = cc.v2();
    }

    start() {
        this.registerBuglyLog();
        this.registerCustomEvent();
    }


    update(dt) {
    }

    /**注册Bugly的事件上报 */
    registerBuglyLog() {
        // if (sys.isMobile) {
        //     window['__errorHandler'] = function (errorMessage, file, line, message, error) {
        //         let exception = {
        //             version: Boot.hotVersion,
        //             errorMessage: errorMessage,
        //             file: file,
        //             line: line,
        //             message: message,
        //             error: error,
        //         };
        //         if (window['exception'] != JSON.stringify(exception)) {
        //             window['exception'] = JSON.stringify(exception);
        //             //发送请求上报异常
        //             NativeUtils.cocosCallNative(NativeFuncName.buglyLog, window['exception']);
        //         }
        //     };
        // }
    }
    /**注册系统事件 */
    registerCustomEvent() {

        //返回键 和 esc键
        cc.input.on(cc.Input.EventType.KEY_DOWN, (event: cc.EventKeyboard) => {
            if (cc.KeyCode.MOBILE_BACK == event.keyCode || cc.KeyCode.ESCAPE == event.keyCode) {
                GameEventDefine.ON_GAME_KEY_BACK.emit();
                const guildModel: ModelGuide = ModelGuide.getInstance() as ModelGuide;
                if (guildModel.isShowing) {
                    return;
                }
                ViewMgr.getInstance().tryRemoveTopView();
            }
        }, this);


        cc.game.on(cc.Game.EVENT_SHOW, () => {
            Loger.log("====on game show")
            GameEventDefine.ON_GAME_SHOW.emit();
        });
        cc.game.on(cc.Game.EVENT_HIDE, () => {
            Loger.log("====on game hide")
            GameEventDefine.ON_GAME_HIDE.emit();
        });
    }
}